package own.stu.jobgib.playown.alg.heap;

public class HeapDemo {
    private int[] a; // 从1开始存储数据
    private int n; // 堆中可以存储的最大数据个数
    private int count; //堆中已经存储的数据个数

    public HeapDemo(int capacity) {
        a = new int[capacity + 1];
        n = capacity;
        count = 0;
    }

    public void insert(int data) {
        if (count >= n) return; // 堆满了
        count++;

        a[count] = data;
        int i = count;
        while (i / 2 > 0 && a[i] > a[i / 2]) {// 自下往上堆化
            swap(a, i, i / 2);
            i = i / 2;
        }
    }

    private void swap(int[] a, int i, int j) {
        int tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }

    public void removeMax(){
        if(count == 0) return ; // 堆中没有数据
        a[1] = a[count];
        --count;
        heapify(a, count, 1);
    }

    private void heapify(int[] a, int count, int i) {

    }


}
